package com.nitramf.catchmystream.entities;

import com.nitramf.catchmystream.entities.geo.CartesianTierFieldBridgeImpl;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import org.apache.lucene.spatial.geohash.GeoHashUtils;
import org.apache.lucene.spatial.geometry.FloatLatLng;
import org.apache.lucene.spatial.geometry.LatLng;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

/**
 * Represent a location (geo point) at a specific time
 * @author martinfilliau
 */
@Entity
@Indexed(index="locations")
public class LocationPoint extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;

    private Double lat;
    
    private Double lng;

    private int accuracy;

    private Date timestamp;

    private String source;

    private String description;

    private String city;

    private String country;

    @Transient
    @Field
    @FieldBridge(impl=CartesianTierFieldBridgeImpl.class)
    public LatLng getLatLng() {
        if (getLat() == null || getLng() == null) {
            return null;
        }
        LatLng latLng = new FloatLatLng(getLat(), getLng());
        return latLng;
    }

    @Transient
    @Field(index=Index.UN_TOKENIZED, store=Store.YES, name="geohash")
    public String getGeohash() {
        if (getLat() == null || getLng() == null) {
            return null;
        }
        return GeoHashUtils.encode(getLat(), getLng());
    }


    /* GETTERs and SETTERs */

    @Id
    @DocumentId
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @Basic
    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @Basic
    public Double getLng() {
        return lng;
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @Basic
    public int getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(int accuracy) {
        this.accuracy = accuracy;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name="pointTimestamp")
    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @DateBridge(resolution = Resolution.SECOND)
    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @Basic
    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    @Basic
    @Column(length=1000)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Basic
    @Column(length=500)
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Basic
    @Column(length=500)
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


    /* Overriden methods */

    @Override
    public int hashCode() {
        int hash = 0;
        hash += ( id != null ? id.hashCode() : 0 );
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!( object instanceof LocationPoint )) {
            return false;
        }
        LocationPoint other = (LocationPoint) object;
        if (( this.id == null && other.id != null ) || ( this.id != null && !this.id.equals(other.id) )) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.nitramf.catchmystream.entities.LocationPoint[id=" + id + "]";
    }
}
